// -----------------------------------------------------------------------
//  <copyright file="Organization.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/10 18:55:30</last-date>
// -----------------------------------------------------------------------

namespace Liuliu.Fsb.Services.Identity.Domain.Entities;

/// <summary>
/// 实体类：组织机构
/// </summary>
[Description("组织机构")]
public class Organization : EntityBase, IAggregateRoot, ILockable, ICreationTime, ISoftDeletable
{
    /// <summary>
    /// 获取或设置 名称，兄弟节点之间的名称不能重复
    /// </summary>
    public string Name { get; set; } = null!;

    /// <summary>
    /// 获取或设置 机构代码，全局唯一
    /// </summary>
    public string Code { get; set; } = null!;

    /// <summary>
    /// 获取或设置 备注
    /// </summary>
    public string? Remark { get; set; }

    /// <summary>
    /// 获取或设置 树路径，从根节点到当前节点的id组成的路径字符串，用于快速查询当前结点及所有父节点
    /// </summary>
    public string TreePathString { get; set; } = null!;

    /// <summary>
    /// 获取或设置 父组织机构
    /// </summary>
    public long? ParentId { get; set; }

    public bool IsLocked { get; set; }

    public DateTime CreatedTime { get; set; }

    public DateTime DeletedTime { get; set; }

    /// <summary>
    /// 获取或设置 子组织机构集合
    /// </summary>
    public virtual ICollection<Organization> Children { get; set; } = new List<Organization>();

    /// <summary>
    /// 获取或设置 用户集合，组织机构与用户是多对多关系
    /// </summary>
    public virtual ICollection<User> Users { get; set; } = new List<User>();

    public void SetTreePath(string? parentTreePath)
    {
        if (Id == 0)
        {
            this.SetId();
        }
        TreePathString = parentTreePath == null ? $"{Id}" : $"{parentTreePath},{Id}";
    }
}
